package com.bcpapp.project.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import com.bcpapp.project.model.dto.post.PostBuyTicketRequest;
import com.bcpapp.project.model.dto.post.PostWicketRequest;
import com.bcpapp.project.service.TicketService;
import com.bcpapp.project.utils.FileUtils;
import com.bcpapp.project.utils.SM4Util;
import com.bcpapp.project.utils.TimeUtils;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Map;


@Service
public class TicketServiceImpl implements TicketService {


    @Value("${file.defaultTicketPath}")
    private String defaultTicketPath;

    @Value("${file.defaultWicketPath}")
    private String defaulWicketPath;

    @Value("${key}")
    private String key;

    @Value("${file.fileMapBuyTicketPath}")
    private String fileMapBuyTicketPath;

    @Value("${file.fileMapCheckTicketPath}")
    private String fileMapCheckTicketPath;

    @Override
    public void postBuyTicketService(PostBuyTicketRequest postBuyTicketRequest) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
        String dateName = TimeUtils.getDateFormat();
        System.out.println("解密前的————————————————————-"+postBuyTicketRequest.getGmsfhm());
        postBuyTicketRequest.setGmsfhm(SM4Util.decrypt(key, postBuyTicketRequest.getGmsfhm()));
        System.out.println("解密后的————————————————————-"+postBuyTicketRequest.getGmsfhm());

        if (StrUtil.isNotBlank(postBuyTicketRequest.getLxdh())) {
            postBuyTicketRequest.setLxdh(SM4Util.decrypt(key, postBuyTicketRequest.getLxdh()));
            System.out.println("解密后的————————————————————-"+postBuyTicketRequest.getLxdh());
        }

        String filename =  dateName + "_" + "0";

        Map<String, String> stringStringMap = FileUtils.readFileData(fileMapBuyTicketPath);


        if (MapUtil.isNotEmpty(stringStringMap) &&  stringStringMap.containsKey(postBuyTicketRequest.getYygsmc())){
            defaultTicketPath = stringStringMap.get(postBuyTicketRequest.getYygsmc());
            String s = StrUtil.subBetween(defaultTicketPath, "buyTicket/", "/");
            filename = s +"_"+filename;


        }

//        File file = new File(defaultTicketPath + "/" + filename + ".csv");
//        FileUtil.appendString(postBuyTicketRequest.toStringt(), file, CharsetUtil.UTF_8);
//        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);


        try (OutputStreamWriter writer = new OutputStreamWriter(
                new FileOutputStream(defaultTicketPath + "/" + filename + ".csv"), StandardCharsets.UTF_8)) {
            StatefulBeanToCsv<PostBuyTicketRequest> beanToCsv = new StatefulBeanToCsvBuilder<PostBuyTicketRequest>(writer).build();
            beanToCsv.write(postBuyTicketRequest);
        } catch (IOException | CsvException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void postCheckTicketService(PostWicketRequest postWicketRequest) throws NoSuchPaddingException, NoSuchAlgorithmException, IOException, BadPaddingException, IllegalBlockSizeException, NoSuchProviderException, InvalidKeyException {
        String dateName = TimeUtils.getDateFormat();

        // String Gmsfhm = ;
        postWicketRequest.setGmsfhm(SM4Util.decrypt(key, postWicketRequest.getGmsfhm()));
        if (StrUtil.isNotBlank(postWicketRequest.getLxdh())) {
            postWicketRequest.setLxdh(SM4Util.decrypt(key, postWicketRequest.getLxdh()));
        }

        String filename =  dateName + "_" + "0";


        // String Lxdh = SM4Util.decrypt("key", postBuyTicketRequest.getLxdh());

        Map<String, String> stringStringMap = FileUtils.readFileData(fileMapCheckTicketPath);


        if (MapUtil.isNotEmpty(stringStringMap) &&  stringStringMap.containsKey(postWicketRequest.getYygsmc())){
            defaulWicketPath = stringStringMap.get(postWicketRequest.getYygsmc());
            String s = StrUtil.subBetween(defaulWicketPath, "checkTicket/", "/");
            filename = s +"_"+filename;
        }


        File file = new File(defaulWicketPath + "/" + filename + ".bcp");
        FileUtil.appendString(postWicketRequest.toStringt(), file, CharsetUtil.UTF_8);
        FileUtil.appendString("\n", file, CharsetUtil.UTF_8);

    }
}
